package com.ourteam.product.ui;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.apache.commons.lang.BooleanUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.ourteam.product.ui.dao.IProductUIActionDAO;
import com.ourteam.product.ui.dao.IProductUIActionGroupRelateDAO;
import com.ourteam.product.ui.dao.IProductUIFormFieldDAO;
import com.ourteam.product.ui.dao.ProductUIActionDAOQueryBean;
import com.ourteam.product.ui.dao.ProductUIActionGroupDAOQueryBean;
import com.ourteam.product.ui.dao.ProductUIActionGroupRelateDAOQueryBean;
import com.ourteam.product.ui.dao.ProductUIFormDAOQueryBean;
import com.ourteam.product.ui.dao.ProductUIFormFieldDAOQueryBean;
import com.ourteam.product.ui.dao.ProductUIFormLayoutDataDAOQueryBean;
import com.ourteam.product.ui.dao.ProductUIListDAOQueryBean;
import com.ourteam.product.ui.dao.ProductUIListOptionDAOQueryBean;
import com.ourteam.product.ui.dao.ProductUITabDAOQueryBean;
import com.ourteam.product.ui.dao.ProductUITableColumnDAOQueryBean;
import com.ourteam.product.ui.dao.ProductUITableColumnIconDAOQueryBean;
import com.ourteam.product.ui.dao.ProductUITableDAOQueryBean;
import com.ourteam.product.ui.dao.ProductUITreeNodeDAOQueryBean;
import com.ourteam.product.ui.dao.impl.ProductUIListOptionDAO;
import com.ourteam.product.ui.dao.impl.ProductUITableColumnDAO;
import com.ourteam.product.ui.domain.ProductUIActionBean;
import com.ourteam.product.ui.domain.ProductUIActionGroupBean;
import com.ourteam.product.ui.domain.ProductUIActionGroupRelateBean;
import com.ourteam.product.ui.domain.ProductUIFormBean;
import com.ourteam.product.ui.domain.ProductUIFormFieldBean;
import com.ourteam.product.ui.domain.ProductUIFormLayoutDataBean;
import com.ourteam.product.ui.domain.ProductUIListBean;
import com.ourteam.product.ui.domain.ProductUIListOptionBean;
import com.ourteam.product.ui.domain.ProductUITabBean;
import com.ourteam.product.ui.domain.ProductUITabItemBean;
import com.ourteam.product.ui.domain.ProductUITableBean;
import com.ourteam.product.ui.domain.ProductUITableColumnBean;
import com.ourteam.product.ui.domain.ProductUITableColumnIconBean;
import com.ourteam.product.ui.domain.ProductUITreeNodeBean;
import com.ourteam.product.ui.domain.ProductUITreeNodeBuilderBean;
import com.ourteam.product.ui.service.IProductUIService;
import com.ourteam.product.ui.service.ProductUIServiceFactory;

import net.ui.UIConfigFactory;
import net.ui.model.action.ActionBean;
import net.ui.model.action.ActionGroupBean;
import net.ui.model.action.ActionSetBean;
import net.ui.model.constlist.ListData;
import net.ui.model.constlist.ListDataContainer;
import net.ui.model.form.FormBean;
import net.ui.model.form.FormPropertyBean;
import net.ui.model.form.FormSetConfig;
import net.ui.model.form.LayoutDataBean;
import net.ui.model.tab.TabConfig;
import net.ui.model.tab.TabProperty;
import net.ui.model.tab.TabbedConfig;
import net.ui.model.tab.TabbedPaneConfig;
import net.ui.model.table.ColumnBean;
import net.ui.model.table.ColumnDataBean;
import net.ui.model.table.TableBean;
import net.ui.model.table.TableConfig;
import net.ui.model.tree.NodeBean;
import net.ui.model.tree.SubNodeBuilderBean;
import net.ui.model.tree.TreeBean;
import net.ui.model.tree.TreeNodeBean;

public class ProductUIConfigFactory extends UIConfigFactory implements ApplicationContextAware {

	/**
	 * Logger for this class
	 */
	private static final Log logger = LogFactory.getLog(ProductUIConfigFactory.class);

	private static final IProductUIService PRODUCT_UI_SERVICE = ProductUIServiceFactory.getProductUIService();

	private static final String[] UI_TAB = new String[] { "UI_TAB" };

	private static final String[] UI_TABLE = new String[] { "UI_TABLE" };

	private static final String[] UI_FORM = new String[] { "UI_FORM" };

	private static final String[] UI_ACTION = new String[] { "UI_ACTION" };

	private static final String[] UI_LIST = new String[] { "UI_LIST" };

	private static final String[] UI_ICON = new String[] { "UI_ICON" };

	private ApplicationContext applicationContext;

	public ProductUIConfigFactory() {
		super("ProductUI");
	}

	protected String getUIFactoryTag() {
		return "ProductUI";
	}

	public void setApplicationContext(ApplicationContext applicationContext) {
		this.applicationContext = applicationContext;
	}

	@Override
	public String[] getTabConfigFilePaths() {
		return UI_TAB;
	}

	@Override
	public String[] getTableConfigFilePaths() {
		return UI_TABLE;
	}

	@Override
	public String[] getFormConfigFilePaths() {
		return UI_FORM;
	}

	@Override
	public String[] getListConfigFilePaths() {
		return UI_LIST;
	}

	@Override
	public String[] getActionConfigFilePaths() {
		return UI_ACTION;
	}

	@Override
	public String[] getIconLibFilePaths() {
		return UI_ICON;
	}

	@Override
	protected long getResourceLastModfiy(String resourcePath) throws Exception {

		long lastModifyTime = 0;

		Date date = null;

		if (UI_TAB[0].equals(resourcePath)) {

			date = PRODUCT_UI_SERVICE.getProductUITabLastModifyTime(0);

		} else if (UI_FORM[0].equals(resourcePath)) {

			date = PRODUCT_UI_SERVICE.getProductUIFormLastModifyTime(0);

		} else if (UI_TABLE[0].equals(resourcePath)) {

			date = PRODUCT_UI_SERVICE.getProductUITableLastModifyTime(0);

		} else if (UI_ACTION[0].equals(resourcePath)) {

			Date actionGroupDate = PRODUCT_UI_SERVICE.getProductUIActionGroupLastModifyTime(0);

			Date actionDate = PRODUCT_UI_SERVICE.getProductUIActionLastModifyTime(0);

			if (actionGroupDate != null && actionDate != null) {

				date = actionGroupDate.after(actionDate) ? actionGroupDate : actionDate;

			} else if (actionGroupDate == null && actionDate != null) {
				date = actionDate;
			} else {
				date = actionGroupDate;
			}

		} else if (UI_LIST[0].equals(resourcePath)) {

			date = PRODUCT_UI_SERVICE.getProductUIListLastModifyTime(0);

		} else if (UI_ICON[0].equals(resourcePath)) {
			date = PRODUCT_UI_SERVICE.getProductUIImageLastModifyTime(0);
		} else {
			date = PRODUCT_UI_SERVICE.getProductUITreeLastModifyTime(Long.parseLong(resourcePath));
		}

		if (date != null) {
			lastModifyTime = date.getTime();
		}

		return lastModifyTime;
	}

	@Override
	protected InputStream getConfigResource(String resourcePath) throws Exception {

		String configStr = null;

		if (UI_TAB[0].equals(resourcePath)) {

			configStr = loadTabConfig(0);

		} else if (UI_FORM[0].equals(resourcePath)) {

			configStr = loadFormConfig(0);

		} else if (UI_TABLE[0].equals(resourcePath)) {

			configStr = loadTableConfig(0);

		} else if (UI_ACTION[0].equals(resourcePath)) {

			configStr = loadActionConfig(0);

		} else if (UI_LIST[0].equals(resourcePath)) {

			configStr = loadListConfig(0);

		} else {
			configStr = loadTreeConfig(Long.parseLong(resourcePath));
		}

		if (StringUtils.isNotBlank(configStr)) {

			ByteArrayInputStream inputStream = new ByteArrayInputStream(configStr.getBytes());

			return inputStream;

		} else {

			return null;
		}
	}

	@Override
	public String getLocalMessage(String key, Object[] args, String defaltMessage, Locale locale) {

		return this.applicationContext.getMessage(key, args, defaltMessage, locale);
	}

	public static String loadTreeConfig(long id) throws Exception {

		StringWriter writer = new StringWriter();

		TreeBean treeBean = new TreeBean();

		// ProductUITreeBean productUITreeBean = PRODUCT_UI_SERVICE
		// .getProductUITreeById(id);

		ProductUITreeNodeDAOQueryBean queryBean = new ProductUITreeNodeDAOQueryBean();

		queryBean.createCriteria().andTreeIdEqualTo(id);

		ProductUITreeNodeBean[] treeNodeBeans = PRODUCT_UI_SERVICE.queryProductUITreeNodes(queryBean);

		for (int i = 0; i < treeNodeBeans.length; i++) {

			ProductUITreeNodeBean productUITreeNodeBean = treeNodeBeans[i];

			NodeBean nodeBean = new NodeBean();

			nodeBean.setDesc(productUITreeNodeBean.getRemarks());

			nodeBean.setId(productUITreeNodeBean.getCode());

			nodeBean.setImage(productUITreeNodeBean.getIcon());

			nodeBean.setLabel(productUITreeNodeBean.getTitle());

			nodeBean.setLabelKey(productUITreeNodeBean.getTitleKey());

			nodeBean.setLeaf(BooleanUtils.toBoolean(productUITreeNodeBean.getIsLeaf()));

			ProductUITreeNodeBuilderBean[] nodeBuilderBeans = PRODUCT_UI_SERVICE
					.getProductUITreeNodeBuilders(productUITreeNodeBean.getId());

			for (int j = 0; j < nodeBuilderBeans.length; j++) {
				ProductUITreeNodeBuilderBean productUITreeNodeBuilderBean = nodeBuilderBeans[j];

				SubNodeBuilderBean subNodeBuilderBean = new SubNodeBuilderBean();

				subNodeBuilderBean.setId(productUITreeNodeBuilderBean.getCode());

				subNodeBuilderBean.setDesc(productUITreeNodeBuilderBean.getRemarks());

				subNodeBuilderBean.setClassName(productUITreeNodeBuilderBean.getNodeBuilder());

				nodeBean.addSubNodeBuilder(subNodeBuilderBean);
			}

			treeBean.addNodeDefine(nodeBean);
		}

		ProductUITreeNodeBean[] rootNodeBeans = PRODUCT_UI_SERVICE.getProductUITreeRootNodes(id, 0);

		for (int i = 0; i < rootNodeBeans.length; i++) {

			ProductUITreeNodeBean productUITreeNodeBean = rootNodeBeans[i];

			TreeNodeBean rootNode = new TreeNodeBean();

			rootNode.setId(String.valueOf(productUITreeNodeBean.getId()));

			loadSubRootTreeNode(rootNode, productUITreeNodeBean);

			treeBean.addRootNode(rootNode);

		}

		UIConfigUtils.saveUITreeConfig(treeBean, writer);

		return writer.toString();
	}

	public static void loadSubRootTreeNode(TreeNodeBean rootNode, ProductUITreeNodeBean treeNodeBean) throws Exception {

		ProductUITreeNodeBean[] rootNodeBeans = PRODUCT_UI_SERVICE.getProductUITreeRootNodes(treeNodeBean.getTreeId(),
				treeNodeBean.getId());

		for (int i = 0; i < rootNodeBeans.length; i++) {
			ProductUITreeNodeBean productUITreeNodeBean = rootNodeBeans[i];

			TreeNodeBean subNode = new TreeNodeBean();

			subNode.setId(productUITreeNodeBean.getCode());

			loadSubRootTreeNode(subNode, productUITreeNodeBean);

			rootNode.addSubNode(subNode);

			loadSubRootTreeNode(subNode, productUITreeNodeBean);
		}

	}

	public static String loadListConfig(long uiDomain) throws Exception {

		StringWriter writer = new StringWriter();

		Map<Long, List<ProductUIListOptionBean>> listOptionMap = new HashMap<>();

		ProductUIListOptionDAOQueryBean optionDAOQueryBean = new ProductUIListOptionDAOQueryBean();

		optionDAOQueryBean.setSortProperty(ProductUIListOptionDAO.ListId);

		optionDAOQueryBean.setSortProperty(ProductUIListOptionDAO.SortIndex);

		ProductUIListOptionBean[] listOptionBeans = PRODUCT_UI_SERVICE.queryProductUIListOptions(optionDAOQueryBean);

		for (int i = 0; i < listOptionBeans.length; i++) {

			if (listOptionMap.containsKey(listOptionBeans[i].getListId()) == false) {
				listOptionMap.put(listOptionBeans[i].getListId(), new ArrayList<>());
			}

			listOptionMap.get(listOptionBeans[i].getListId()).add(listOptionBeans[i]);
		}

		List<ListDataContainer> listDataContainers = new ArrayList<ListDataContainer>();

		ProductUIListDAOQueryBean queryBean = new ProductUIListDAOQueryBean();

		ProductUIListDAOQueryBean.Criteria criteria = queryBean.createCriteria();

		if (uiDomain != 0) {
			criteria.andDomainIdEqualTo(uiDomain);
		}

		ProductUIListBean[] productUIListBeans = PRODUCT_UI_SERVICE.queryProductUILists(queryBean);

		for (int i = 0; i < productUIListBeans.length; i++) {
			ProductUIListBean productUIListBean = productUIListBeans[i];

			ListDataContainer listDataContainer = new ListDataContainer();

			listDataContainer.setDataProperty(productUIListBean.getDataProperty());

			listDataContainer.setDesc(productUIListBean.getRemarks());

			listDataContainer.setLabelProperty(productUIListBean.getLabelProperty());

			listDataContainer.setIconProperty(productUIListBean.getIconProperty());

			listDataContainer.setSearchProperty(productUIListBean.getSearchProperty());

			// listDataContainer.setName(String.valueOf(productUIListBean.getId()));

			listDataContainer.setName(productUIListBean.getCode());

			listDataContainer.setType(productUIListBean.getType());

			listDataContainer.setScript(productUIListBean.getScript());

//			ProductUIListOptionBean[] listOptionBeans = PRODUCT_UI_SERVICE
//					.getProductUIListOptions(productUIListBean.getId());

			List<ProductUIListOptionBean> listOptionBeanList = listOptionMap.get(productUIListBean.getId());

			for (int j = 0; listOptionBeanList != null && j < listOptionBeanList.size(); j++) {

				ProductUIListOptionBean productUIListOptionBean = listOptionBeanList.get(j);

				ListData listData = new ListData();

				listData.setListData(productUIListOptionBean.getDataValue(), productUIListOptionBean.getDataLabel(),
						productUIListOptionBean.getDataIcon());

				listDataContainer.addListData(listData);
			}

			listDataContainers.add(listDataContainer);
		}

		UIConfigUtils.saveUISelectListConfig(listDataContainers, writer);

		return writer.toString();
	}

	public static String loadActionConfig(long uiDomain) throws Exception {

		StringWriter writer = new StringWriter();

		Map<Long, ProductUIActionBean> uiActionMap = new HashMap<>();

		ActionSetBean actionSetBean = new ActionSetBean();

		ProductUIActionDAOQueryBean actionQueryBean = new ProductUIActionDAOQueryBean();

		ProductUIActionDAOQueryBean.Criteria criteria = actionQueryBean.createCriteria();

		if (uiDomain != 0) {
			criteria.andDomainIdEqualTo(uiDomain);
		}

		criteria.andParentIdEqualTo(0);

		ProductUIActionBean[] actionBeans = PRODUCT_UI_SERVICE.queryProductUIActions(actionQueryBean);

		for (int i = 0; i < actionBeans.length; i++) {
			ProductUIActionBean productUIActionBean = actionBeans[i];

			ActionBean actionBean = new ActionBean();

			// actionBean.setId(String.valueOf(productUIActionBean.getId()));

			actionBean.setId(productUIActionBean.getCode());

			actionBean.setDesc(productUIActionBean.getRemarks());

			actionBean.setDomain(productUIActionBean.getDomainCode());

			actionBean.setHandler(productUIActionBean.getHandler());

			actionBean.setIcon(productUIActionBean.getIcon());

			actionBean.setStyle(productUIActionBean.getStyle());

			actionBean.setLabel(productUIActionBean.getLabelText());

			actionBean.setLabelKey(productUIActionBean.getLabelKey());

			if (StringUtils.isBlank(productUIActionBean.getConfirmInfo())) {
				actionBean.setConfirmInfoKey(productUIActionBean.getConfirmInfo());
			} else {
				actionBean.setConfirmInfoKey(productUIActionBean.getConfirmInfo());
			}

			if (StringUtils.isNotBlank(productUIActionBean.getVisibleScript())) {
				actionBean.setVisible(productUIActionBean.getVisibleScript());
			}

			if (StringUtils.isNotBlank(productUIActionBean.getDisableScript())) {
				actionBean.setDisabled(productUIActionBean.getDisableScript());
			}

			if (productUIActionBean.getMenuListId() != 0) {
				actionBean.setMenuListId(productUIActionBean.getListCode());
			}

			if (StringUtils.isNotBlank(productUIActionBean.getRemarks())) {
				actionBean.setDesc(productUIActionBean.getRemarks());
			}

			actionBean.setType(productUIActionBean.getType());

			loadSubAction(actionBean, productUIActionBean);

			uiActionMap.put(productUIActionBean.getId(), productUIActionBean);

			actionSetBean.addAction(actionBean);
		}

		ProductUIActionGroupRelateDAOQueryBean actionGroupRelateQueryBean = new ProductUIActionGroupRelateDAOQueryBean();

		ProductUIActionGroupDAOQueryBean queryBean = new ProductUIActionGroupDAOQueryBean();

		ProductUIActionGroupDAOQueryBean.Criteria criteria2 = queryBean.createCriteria();

		if (uiDomain != 0) {
			criteria2.andDomainIdEqualTo(uiDomain);
		}

		ProductUIActionGroupBean[] actionGroupBeans = PRODUCT_UI_SERVICE.queryProductUIActionGroups(queryBean);

		for (int i = 0; i < actionGroupBeans.length; i++) {

			ProductUIActionGroupBean productUIActionGroupBean = actionGroupBeans[i];

			ActionGroupBean actionGroupBean = new ActionGroupBean();

			actionGroupBean.setId(productUIActionGroupBean.getActionGroupCode());

			actionGroupBean.setDesc(productUIActionGroupBean.getRemarks());

			actionGroupRelateQueryBean.clear();

			actionGroupRelateQueryBean.setSortProperty(IProductUIActionGroupRelateDAO.SortIndex);

			actionGroupRelateQueryBean.createCriteria().andActionGroupIdEqualTo(productUIActionGroupBean.getId());

			ProductUIActionGroupRelateBean[] actionGroupRelateBeans = PRODUCT_UI_SERVICE
					.queryProductUIActionGroupRelates(actionGroupRelateQueryBean);

			for (int j = 0; j < actionGroupRelateBeans.length; j++) {

				ProductUIActionGroupRelateBean productUIActionGroupRelateBean = actionGroupRelateBeans[j];

				ProductUIActionBean uiActionBean = null;

				if (uiActionMap.containsKey(productUIActionGroupRelateBean.getRelatedActionId())) {
					uiActionBean = uiActionMap.get(productUIActionGroupRelateBean.getRelatedActionId());
				} else {
					uiActionBean = PRODUCT_UI_SERVICE
							.getProductUIActionById(productUIActionGroupRelateBean.getRelatedActionId());
				}

				if (uiActionBean == null) {
					logger.error(
							"Can not found ActionBean By ID :" + productUIActionGroupRelateBean.getRelatedActionId());
					continue;
				}

				ActionBean actionBean = new ActionBean();

				// actionBean.setRef(String.valueOf(productUIActionGroupRelateBean.getRelatedActionId()));

				actionBean.setRef(uiActionBean.getCode());

				actionBean.setType(uiActionBean.getType());

				if (StringUtils.isNotEmpty(productUIActionGroupRelateBean.getConfirmInfo())) {
					actionBean.setConfirmInfoKey(productUIActionGroupRelateBean.getConfirmInfo());
				} else {
					actionBean.setConfirmInfoKey(uiActionBean.getConfirmInfo());
				}

				if (StringUtils.isNotEmpty(productUIActionGroupRelateBean.getStyle())) {
					actionBean.setStyle(productUIActionGroupRelateBean.getStyle());
				} else {
					actionBean.setStyle(uiActionBean.getStyle());
				}

				if (StringUtils.isNotEmpty(productUIActionGroupRelateBean.getHandler())) {
					actionBean.setHandler(productUIActionGroupRelateBean.getHandler());
				} else {
					actionBean.setHandler(uiActionBean.getHandler());
				}

				if (StringUtils.isNotEmpty(productUIActionGroupRelateBean.getIcon())) {
					actionBean.setIcon(productUIActionGroupRelateBean.getIcon());
				} else {
					actionBean.setIcon(uiActionBean.getIcon());
				}

				if (StringUtils.isNotEmpty(productUIActionGroupRelateBean.getLabelText())) {
					actionBean.setLabel(productUIActionGroupRelateBean.getLabelText());
				} else {
					actionBean.setLabel(uiActionBean.getLabelText());
				}

				if (StringUtils.isNotEmpty(productUIActionGroupRelateBean.getLabelKey())) {
					actionBean.setLabelKey(productUIActionGroupRelateBean.getLabelKey());
				} else {
					actionBean.setLabelKey(uiActionBean.getLabelKey());
				}

				if (StringUtils.isNotEmpty(productUIActionGroupRelateBean.getRemarks())) {
					actionBean.setDesc(productUIActionGroupRelateBean.getRemarks());
				} else {
					actionBean.setDesc(uiActionBean.getRemarks());
				}

				if (StringUtils.isNotEmpty(productUIActionGroupRelateBean.getConfirmInfo())) {
					actionBean.setNeedComfirm(productUIActionGroupRelateBean.getConfirmInfo());
				} else {
					actionBean.setNeedComfirm(uiActionBean.getConfirmInfo());
				}

				if (StringUtils.isNotEmpty(productUIActionGroupRelateBean.getVisibleScript())) {
					actionBean.setVisible(productUIActionGroupRelateBean.getVisibleScript());
				} else if (StringUtils.isNotEmpty(uiActionBean.getVisibleScript())) {
					actionBean.setVisible(uiActionBean.getVisibleScript());
				}

				if (StringUtils.isNotEmpty(productUIActionGroupRelateBean.getDisableScript())) {
					actionBean.setDisabled(productUIActionGroupRelateBean.getDisableScript());
				} else if (StringUtils.isNotEmpty(uiActionBean.getDisableScript())) {
					actionBean.setDisabled(uiActionBean.getDisableScript());
				}

				if (productUIActionGroupRelateBean.getMenuListId() != 0) {
					actionBean.setMenuListId(productUIActionGroupRelateBean.getListCode());
				} else if (uiActionBean.getMenuListId() != 0) {
					actionBean.setMenuListId(uiActionBean.getListCode());
				}

				actionQueryBean.clear();
				actionQueryBean.setSortProperty(IProductUIActionDAO.SortIndex);
				actionQueryBean.createCriteria().andParentIdEqualTo(uiActionBean.getId());

				ProductUIActionBean[] uiSubActionBean = PRODUCT_UI_SERVICE.queryProductUIActions(actionQueryBean);

				for (int k = 0; i < uiSubActionBean.length; k++) {

					ActionBean subActionBean = new ActionBean();

					// subActionBean.setRef(String.valueOf(uiSubActionBean[k].getId()));

					subActionBean.setRef(uiSubActionBean[k].getCode());

					subActionBean.setType(uiSubActionBean[k].getType());

					subActionBean.setConfirmInfoKey(uiSubActionBean[k].getConfirmInfo());

					subActionBean.setHandler(uiSubActionBean[k].getHandler());

					subActionBean.setIcon(uiSubActionBean[k].getIcon());

					subActionBean.setLabel(uiSubActionBean[k].getLabelText());

					subActionBean.setLabelKey(uiSubActionBean[k].getLabelKey());

					subActionBean.setDesc(uiSubActionBean[k].getRemarks());

					actionBean.addSubActionBean(subActionBean);
				}

				actionGroupBean.addActionBean(actionBean);

			}

			actionSetBean.addActionGroup(actionGroupBean);
		}

		UIConfigUtils.saveUIActionConfig(actionSetBean, writer);

		// System.out.println(writer);

		return writer.getBuffer().toString();
	}

	public static void loadSubAction(ActionBean parentActionBean, ProductUIActionBean parentUIActionBean)
			throws Exception {

		ProductUIActionDAOQueryBean actionQueryBean = new ProductUIActionDAOQueryBean();

		actionQueryBean.createCriteria().andParentIdEqualTo(parentUIActionBean.getId());

		ProductUIActionBean[] actionBeans = PRODUCT_UI_SERVICE.queryProductUIActions(actionQueryBean);

		for (int i = 0; i < actionBeans.length; i++) {

			ProductUIActionBean productUIActionBean = actionBeans[i];

			ActionBean actionBean = new ActionBean();

			// actionBean.setId(String.valueOf(productUIActionBean.getId()));

			actionBean.setId(productUIActionBean.getCode());

			actionBean.setType(productUIActionBean.getType());

			actionBean.setDesc(productUIActionBean.getRemarks());

			actionBean.setDomain(productUIActionBean.getDomainCode());

			actionBean.setHandler(productUIActionBean.getHandler());

			actionBean.setIcon(productUIActionBean.getIcon());

			actionBean.setLabel(productUIActionBean.getLabelText());

			actionBean.setLabelKey(productUIActionBean.getLabelKey());

			if (StringUtils.isBlank(productUIActionBean.getConfirmInfo())) {
				actionBean.setConfirmInfoKey(productUIActionBean.getConfirmInfo());
			} else {
				actionBean.setConfirmInfoKey(productUIActionBean.getConfirmInfoKey());
			}

			if (StringUtils.isNotBlank(productUIActionBean.getVisibleScript())) {
				actionBean.setVisible(productUIActionBean.getVisibleScript());
			}

			if (productUIActionBean.getMenuListId() != 0) {
				actionBean.setMenuListId(productUIActionBean.getListCode());
			}

			if (StringUtils.isNotBlank(productUIActionBean.getRemarks())) {
				actionBean.setDesc(productUIActionBean.getRemarks());
			}

			loadSubAction(actionBean, productUIActionBean);

			parentActionBean.addSubActionBean(actionBean);

		}

	}

	public static String loadTableConfig(long domainId) throws Exception {

		Map<Long, List<ProductUITableColumnBean>> uiTableColumnMap = new HashMap<>();

		Map<Long, List<ProductUITableColumnIconBean>> uiTableColumnIconMap = new HashMap<>();

		ProductUITableColumnDAOQueryBean tableColumnDAOQueryBean = new ProductUITableColumnDAOQueryBean();

		tableColumnDAOQueryBean.setSortProperty(ProductUITableColumnDAO.TableId);
		tableColumnDAOQueryBean.setSortProperty(ProductUITableColumnDAO.SortIndex);

		ProductUITableColumnBean[] tableColumnBeans = PRODUCT_UI_SERVICE
				.queryProductUITableColumns(tableColumnDAOQueryBean);

		for (int i = 0; i < tableColumnBeans.length; i++) {

			if (uiTableColumnMap.containsKey(tableColumnBeans[i].getTableId()) == false) {
				uiTableColumnMap.put(tableColumnBeans[i].getTableId(), new ArrayList<>());
			}

			uiTableColumnMap.get(tableColumnBeans[i].getTableId()).add(tableColumnBeans[i]);
		}

		ProductUITableColumnIconBean[] tableColumnIconBeans = PRODUCT_UI_SERVICE
				.queryProductUITableColumnIcons(new ProductUITableColumnIconDAOQueryBean());

		for (int i = 0; i < tableColumnIconBeans.length; i++) {

			if (uiTableColumnIconMap.containsKey(tableColumnIconBeans[i].getColumnId()) == false) {
				uiTableColumnIconMap.put(tableColumnIconBeans[i].getColumnId(), new ArrayList<>());
			}

			uiTableColumnIconMap.get(tableColumnIconBeans[i].getColumnId()).add(tableColumnIconBeans[i]);
		}

		TableConfig tableConfig = new TableConfig();

		ProductUITableDAOQueryBean queryBean = new ProductUITableDAOQueryBean();

		ProductUITableDAOQueryBean.Criteria criteria = queryBean.createCriteria();

		if (domainId != 0) {
			criteria.andDomainIdEqualTo(domainId);
		}

		ProductUITableBean[] tableBeans = PRODUCT_UI_SERVICE.queryProductUITables(queryBean);

		for (int i = 0; i < tableBeans.length; i++) {

			ProductUITableBean productUITableBean = tableBeans[i];

			TableBean tableBean = new TableBean();

			tableBean.setDesc(productUITableBean.getRemarks());

			tableBean.setId(productUITableBean.getCode());

			tableBean.setRowId(productUITableBean.getRowId());

//			ProductUITableColumnBean[] tableColumnBeans = PRODUCT_UI_SERVICE
//					.getProductUITableColumns(productUITableBean.getId());

			List<ProductUITableColumnBean> tableColumnBeanList = uiTableColumnMap.get(productUITableBean.getId());

			for (int j = 0;tableColumnBeanList != null && j < tableColumnBeanList.size(); j++) {
				ProductUITableColumnBean productUITableColumnBean = tableColumnBeanList.get(j);

				ColumnBean columnBean = new ColumnBean();

				columnBean.setProperty(productUITableColumnBean.getCode());

				columnBean.setAlign(productUITableColumnBean.getAlign());

				columnBean.setCanFilter(BooleanUtils.toBoolean(productUITableColumnBean.getCanFilter()));

				columnBean.setCanReOrder(BooleanUtils.toBoolean(productUITableColumnBean.getCanReOrder()));

				columnBean.setCanResize(BooleanUtils.toBoolean(productUITableColumnBean.getCanResize()));

				columnBean.setCanSort(BooleanUtils.toBoolean(productUITableColumnBean.getCanSort()));

				columnBean.setFrozen(BooleanUtils.toBoolean(productUITableColumnBean.getFrozen()));

				columnBean.setCellEditor(productUITableColumnBean.getCellEditor());

				columnBean.setDataType(productUITableColumnBean.getDataType());

				columnBean.setIcon(productUITableColumnBean.getTitleIcon());

				columnBean.setLabel(productUITableColumnBean.getTitle());

				columnBean.setLabelKey(productUITableColumnBean.getTitleKey());

				if (StringUtils.isNotBlank(productUITableColumnBean.getListDataSource())
						&& productUITableColumnBean.getListDataSource().equals("0") == false) {

					ProductUIListBean uiListBean = PRODUCT_UI_SERVICE
							.getProductUIListById(Long.parseLong(productUITableColumnBean.getListDataSource()));

					if (uiListBean != null) {
						columnBean.setListDataSource(uiListBean.getCode());
					}

				}

//				if (productUITableColumnBean.getActionGroupId() != 0) {
//					ColumnDataBean columnDataBean = new ColumnDataBean();
//
//					columnDataBean.setLabel("actionGroupId");
//					
//					ProductUIActionGroupBean actionGroupBean = PRODUCT_UI_SERVICE.getProductUIActionGroupById(productUITableColumnBean.getActionGroupId());
//
//					columnDataBean.setData("ProductUI:" + actionGroupBean.getActionGroupCode());
//
//					columnBean.addColumnData(columnDataBean);
//				}

				columnBean.setShowValue(BooleanUtils.toBoolean(productUITableColumnBean.getShowValue()));

				columnBean.setWidth(productUITableColumnBean.getWidth());

				if (StringUtils.isNotBlank(productUITableColumnBean.getFormatValue())) {

					ColumnDataBean columnDataBean = new ColumnDataBean();

					columnDataBean.setLabel("dateFormat");

					columnDataBean.setData(productUITableColumnBean.getFormatValue());

					columnBean.addColumnData(columnDataBean);

				}

//				ProductUITableColumnIconBean[] iconBeans = PRODUCT_UI_SERVICE
//						.getProductUITableColumnIcons(productUITableColumnBean.getId());
				
				List<ProductUITableColumnIconBean> iconBeanList = uiTableColumnIconMap.get(productUITableColumnBean.getId());

				for (int k = 0;iconBeanList != null && k < iconBeanList.size(); k++) {

					ProductUITableColumnIconBean productUITableColumnIconBean = iconBeanList.get(k);

					columnBean.addImage(productUITableColumnIconBean.getDataValue(),
							productUITableColumnIconBean.getDataIcon());

				}

				tableBean.addColumn(columnBean);
			}

			tableConfig.addTable(tableBean);
		}

		StringWriter writer = new StringWriter();

		UIConfigUtils.saveUITableConfig(tableConfig, writer);

		// System.out.println(writer);

		return writer.getBuffer().toString();

	}

	public static String loadFormConfig(long domainId) throws Exception {

		Map<Long, List<ProductUIFormFieldBean>> uiFormFieldMap = new HashMap<>();

		Map<Long, List<ProductUIFormLayoutDataBean>> uiFormFieldLayoutMap = new HashMap<>();

		FormSetConfig formSetConfig = new FormSetConfig();

		ProductUIFormDAOQueryBean queryBean = new ProductUIFormDAOQueryBean();

		ProductUIFormDAOQueryBean.Criteria criteria = queryBean.createCriteria();

		if (domainId != 0) {
			criteria.andDomainIdEqualTo(domainId);
		}

		ProductUIFormBean[] productUIFormBeans = PRODUCT_UI_SERVICE.queryProductUIForms(queryBean);

		ProductUIFormFieldDAOQueryBean formFieldQueryBean = new ProductUIFormFieldDAOQueryBean();

		formFieldQueryBean.createCriteria().andParentIdEqualTo(0);
		formFieldQueryBean.setSortProperty(IProductUIFormFieldDAO.FormId);
		formFieldQueryBean.setSortProperty(IProductUIFormFieldDAO.SortIndex);

		ProductUIFormFieldBean[] formFieldBeans = PRODUCT_UI_SERVICE.queryProductUIFormFields(formFieldQueryBean);

		for (int i = 0; i < formFieldBeans.length; i++) {

			if (uiFormFieldMap.containsKey(formFieldBeans[i].getFormId()) == false) {
				uiFormFieldMap.put(formFieldBeans[i].getFormId(), new ArrayList<>());
			}

			uiFormFieldMap.get(formFieldBeans[i].getFormId()).add(formFieldBeans[i]);
		}

		ProductUIFormLayoutDataBean[] layoutDataBeans = PRODUCT_UI_SERVICE
				.queryProductUIFormLayoutDatas(new ProductUIFormLayoutDataDAOQueryBean());

		for (int i = 0; i < layoutDataBeans.length; i++) {
			if (uiFormFieldLayoutMap.containsKey(layoutDataBeans[i].getFormFieldId()) == false) {
				uiFormFieldLayoutMap.put(layoutDataBeans[i].getFormFieldId(), new ArrayList<>());
			}

			uiFormFieldLayoutMap.get(layoutDataBeans[i].getFormFieldId()).add(layoutDataBeans[i]);
		}

		for (int i = 0; i < productUIFormBeans.length; i++) {
			ProductUIFormBean productUIFormBean = productUIFormBeans[i];

			FormBean formBean = new FormBean();

			// formBean.setId(String.valueOf(productUIFormBean.getId()));

			formBean.setId(productUIFormBean.getCode());

			formBean.setTitle(productUIFormBean.getTitle());

			formBean.setTitleKey(productUIFormBean.getTitleKey());

			formBean.setNumColumns(String.valueOf(productUIFormBean.getNumColumns()));

			formBean.setDesc(productUIFormBean.getRemarks());

//			formFieldQueryBean.clear();
//
//			formFieldQueryBean.createCriteria().andFormIdEqualTo(productUIFormBean.getId()).andParentIdEqualTo(0);
//
//			formFieldQueryBean.setSortProperty(IProductUIFormFieldDAO.SortIndex);

//			ProductUIFormFieldBean[] formFieldBeans = PRODUCT_UI_SERVICE.queryProductUIFormFields(formFieldQueryBean);

			List<ProductUIFormFieldBean> formFieldBeanList = uiFormFieldMap.get(productUIFormBean.getId());

			for (int j = 0; j < formFieldBeanList.size(); j++) {

				ProductUIFormFieldBean productUIFormFieldBean = formFieldBeanList.get(j);

				FormPropertyBean formPropertyBean = new FormPropertyBean();

				formPropertyBean.setColumn(productUIFormFieldBean.getFieldColumn());

				formPropertyBean.setSpan(productUIFormFieldBean.getFieldSpan());

				formPropertyBean.setAllowNull(BooleanUtils.toBooleanObject(productUIFormFieldBean.getAllowEmpty()));

				if (StringUtils.isNotBlank(productUIFormFieldBean.getDataSource())) {
					ProductUIListBean listBean = PRODUCT_UI_SERVICE
							.getProductUIListById(Long.parseLong(productUIFormFieldBean.getDataSource()));
					if (listBean != null) {
						formPropertyBean.setDataSrc(listBean.getCode());
					}

				}

				formPropertyBean.setDesc(productUIFormFieldBean.getRemarks());

				formPropertyBean.setId(productUIFormFieldBean.getCode());

				formPropertyBean.setReadOnly(BooleanUtils.toBoolean(productUIFormFieldBean.getReadOnly()));

				formPropertyBean.setRemenberValue(BooleanUtils.toBoolean(productUIFormFieldBean.getRemenberValue()));

				formPropertyBean.setStyleDesc(productUIFormFieldBean.getStyle());

				formPropertyBean.setTipInfo(productUIFormFieldBean.getTipInfo());

				formPropertyBean.setTitle(productUIFormFieldBean.getTitle());

				formPropertyBean.setTitleKey(productUIFormFieldBean.getTitleKey());

				formPropertyBean.setType(productUIFormFieldBean.getTypeWidget());

				formPropertyBean.setValue(productUIFormFieldBean.getParameterValue());

//				ProductUIFormLayoutDataBean[] layoutDataBeans = PRODUCT_UI_SERVICE
//						.getProductUIFormFieldLayoutDatas(productUIFormFieldBean.getId());

				List<ProductUIFormLayoutDataBean> layoutDataBeanList = uiFormFieldLayoutMap
						.get(productUIFormFieldBean.getId());

				for (int k = 0; layoutDataBeanList != null && k < layoutDataBeanList.size(); k++) {

					ProductUIFormLayoutDataBean productUIFormLayoutDataBean = layoutDataBeanList.get(k);

					LayoutDataBean layoutDataBean = new LayoutDataBean();

					layoutDataBean.setName(productUIFormLayoutDataBean.getName());

					layoutDataBean.setValue(productUIFormLayoutDataBean.getValue());

					formPropertyBean.addLayoutData(layoutDataBean);

				}

				if (BooleanUtils.toBoolean(productUIFormFieldBean.getAllowSubField())) {
					loadSubFormFields(formPropertyBean, productUIFormFieldBean);
				}

				formBean.addFormProperty(formPropertyBean);
			}

			formSetConfig.addForm(formBean);

		}

		StringWriter writer = new StringWriter();

		UIConfigUtils.saveUIFromConfig(formSetConfig, writer);

		// System.out.println(writer);

		return writer.getBuffer().toString();

	}

	public static void loadSubFormFields(FormPropertyBean formPropertyBean,
			ProductUIFormFieldBean productUIFormFieldBean) throws Exception {

		ProductUIFormFieldDAOQueryBean formFieldQueryBean = new ProductUIFormFieldDAOQueryBean();

		formFieldQueryBean.clear();

		formFieldQueryBean.createCriteria().andFormIdEqualTo(productUIFormFieldBean.getFormId())
				.andParentIdEqualTo(productUIFormFieldBean.getId());

		formFieldQueryBean.setSortProperty(IProductUIFormFieldDAO.SortIndex);

		ProductUIFormFieldBean[] formFieldBeans = PRODUCT_UI_SERVICE.queryProductUIFormFields(formFieldQueryBean);

		for (int i = 0; i < formFieldBeans.length; i++) {

			ProductUIFormFieldBean subProductUIFormFieldBean = formFieldBeans[i];

			FormPropertyBean subFormPropertyBean = new FormPropertyBean();

			subFormPropertyBean.setColumn(subProductUIFormFieldBean.getFieldColumn());

			subFormPropertyBean.setSpan(subProductUIFormFieldBean.getFieldSpan());

			subFormPropertyBean.setAllowNull(BooleanUtils.toBooleanObject(subProductUIFormFieldBean.getAllowEmpty()));

			subFormPropertyBean.setDataSrc(subProductUIFormFieldBean.getDataSource());

			if (StringUtils.isNotBlank(subProductUIFormFieldBean.getDataSource())) {
				subFormPropertyBean.setDataSrc(PRODUCT_UI_SERVICE
						.getProductUIListById(Long.parseLong(subProductUIFormFieldBean.getDataSource())).getCode());
			}

			subFormPropertyBean.setDesc(subProductUIFormFieldBean.getRemarks());

			subFormPropertyBean.setId(subProductUIFormFieldBean.getCode());

			subFormPropertyBean.setReadOnly(BooleanUtils.toBoolean(subProductUIFormFieldBean.getReadOnly()));

			subFormPropertyBean.setStyleDesc(subProductUIFormFieldBean.getStyle());

			subFormPropertyBean.setTipInfo(subProductUIFormFieldBean.getTipInfo());

			subFormPropertyBean.setTitle(subProductUIFormFieldBean.getTitle());

			subFormPropertyBean.setTitleKey(subProductUIFormFieldBean.getTitleKey());

			subFormPropertyBean.setType(subProductUIFormFieldBean.getTypeWidget());

			subFormPropertyBean.setValue(subProductUIFormFieldBean.getParameterValue());

			ProductUIFormLayoutDataBean[] layoutDataBeans = PRODUCT_UI_SERVICE
					.getProductUIFormFieldLayoutDatas(subProductUIFormFieldBean.getId());

			for (int k = 0; k < layoutDataBeans.length; k++) {

				ProductUIFormLayoutDataBean productUIFormLayoutDataBean = layoutDataBeans[k];

				LayoutDataBean layoutDataBean = new LayoutDataBean();

				layoutDataBean.setName(productUIFormLayoutDataBean.getName());

				layoutDataBean.setValue(productUIFormLayoutDataBean.getValue());

				subFormPropertyBean.addLayoutData(layoutDataBean);

			}

			if (BooleanUtils.toBoolean(subProductUIFormFieldBean.getAllowSubField())) {
				loadSubFormFields(subFormPropertyBean, subProductUIFormFieldBean);
			}

			formPropertyBean.addSubProperty(subFormPropertyBean);

		}

	}

	public static String loadTabConfig(long domainId) throws Exception {

		TabbedConfig tabbedConfig = new TabbedConfig();

		ProductUITabDAOQueryBean queryBean = new ProductUITabDAOQueryBean();

		ProductUITabDAOQueryBean.Criteria criteria = queryBean.createCriteria();

		if (domainId != 0) {
			criteria.andDomainIdEqualTo(domainId);
		}

		ProductUITabBean[] tabBeans = PRODUCT_UI_SERVICE.queryProductUITabs(queryBean);

		for (int i = 0; i < tabBeans.length; i++) {

			ProductUITabBean productUITabBean = tabBeans[i];

			TabbedPaneConfig tabbedPaneConfig = new TabbedPaneConfig();

			tabbedPaneConfig.setId(productUITabBean.getCode());

			tabbedPaneConfig.setDesc(productUITabBean.getRemarks());

			ProductUITabItemBean[] uiTabItemBeans = PRODUCT_UI_SERVICE.getProductUITabItems(productUITabBean.getId());

			for (int j = 0; j < uiTabItemBeans.length; j++) {

				ProductUITabItemBean productUITabItemBean = uiTabItemBeans[j];

				TabConfig tabConfig = new TabConfig();

				tabConfig.setLabel(productUITabItemBean.getTitle());

				tabConfig.setLabelKey(productUITabItemBean.getTitleKey());

				tabConfig.setIcon(productUITabItemBean.getIcon());

				tabConfig.setName(productUITabItemBean.getCode());

				tabConfig.setCompentclass(productUITabItemBean.getCompentClass());

				if (productUITabItemBean.getFormId() != 0) {

					TabProperty tabProperty = new TabProperty();

					tabProperty.setId("formId");

					tabProperty.setValue(String.valueOf(productUITabItemBean.getFormId()));

					tabConfig.addProperty(tabProperty);

				}

				tabbedPaneConfig.addTab(tabConfig);

			}

			tabbedConfig.addTabbedPane(tabbedPaneConfig);

		}

		StringWriter writer = new StringWriter();

		UIConfigUtils.saveUITabConfig(tabbedConfig, writer);

		return writer.getBuffer().toString();

	}

	@Override
	public String[] getPanelConfigFilePaths() {
		// TODO Auto-generated method stub
		return null;
	}

}
